﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for InvoiceDAO
/// </summary>
public class InvoiceDAO
{
	public InvoiceDAO()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    SqlHelp help = new SqlHelp();

    public DataTable GetContractNo(int n)
    {
        return help.RunDataTableProc("GetContractNoInvoice", new SqlParameter[] { new SqlParameter("@InvoiceType", n) });
    }
    public DataTable GetSoNo(string strCondiction)
    {
        return help.GetDataTable("select distinct T.TradingSoNo from M_Contract C "
            + " join M_SO_Trading T on C.ContractNoMain=T.ContractNoMain and T.TradingSoNo is not null where  C.ContractNoMain='" + strCondiction + "'");
    }

    public DataTable[] GetInvoice(string invoiceNo)
    {
        return help.RunProcGetTables("GetInvoice", new SqlParameter[] { new SqlParameter("@InvoiceNo", invoiceNo) });
    }
    public DataTable GetInvoiceHead(string invoiceNo)
    {
        return help.GetDataTable(string.Format("select InvoiceNo, InvoiceDate,InvoiceAmount,InvoiceDesc,"
            + " convert(float,isnull(InvoiceAmount,0))-(select sum(convert(float,isnull(InvoiceContractAmount,0))) from M_InvoiceContract where InvoiceNo='{0}') leftAmount "
            + " from M_InvoiceNo where InvoiceNo='{0}'", invoiceNo));
    }
    public DataTable GetInvoiceContract(string invoiceNo)
    {
        return help.RunDataTableProc("GetInvoiceContract", new SqlParameter[] { new SqlParameter("@InvoiceNo", invoiceNo) });
    }
   
    public int InvoiceDelete(string invoiceNo)
    {
        return help.RunProcInt("InvoiceDelete", new SqlParameter[] { new SqlParameter("@InvoiceNo", invoiceNo) });
    }
    public int InvoiceContractDelete(int InvoiceContractID)
    {
        return help.RunProcInt("InvoiceContractDelete", new SqlParameter[] { new SqlParameter("@M_InvoiceContractID", InvoiceContractID) });
    }

    public string GetTradingBillingAmount(InvoiceModel model)
    {
        string date = System.DateTime.Now.ToString("yyyy-MM-dd hh:mm");
        SqlParameter[] prams = new SqlParameter[]{
            new SqlParameter("@ContractNoMain",model.ContractNoMain),
            new SqlParameter("@SONo",model.SoNo)
        };
        DataTable dt = help.RunDataTableProc("GetTradingBillingAmount", prams);
        if (dt != null && dt.Rows.Count > 0)
        {
            return dt.Rows[0]["TradingBillingAmount"].ToString();
        }
        return "0";
    }
    public int InvoiceAdd(InvoiceModel model)
    {
        string date = System.DateTime.Now.ToString("yyyy-MM-dd hh:mm");
        SqlParameter[] prams = new SqlParameter[]{
            new SqlParameter("@InvoiceNo",model.InvoiceNo),
            new SqlParameter("@CoCode",model.CoCode),
            new SqlParameter("@ContractNoMain",model.ContractNoMain),
            new SqlParameter("@SONo",model.SoNo),
            new SqlParameter("@ContractNoCust",model.ContractNoMain),
            new SqlParameter("@InvoiceDate",model.InvoiceDate),
            new SqlParameter("@InvoiceAmount",model.InvoiceAmount),
            new SqlParameter("@InvoiceDesc",model.InvoiceDesc),
            new SqlParameter("@DeleteFlag",'0'),
            new SqlParameter("@UserCode",model.UserCode),
            new SqlParameter("@date",date),
            new SqlParameter("@InvoiceContractAmount",model.InvoiceContractAmount),
            new SqlParameter("@InvoiceType",model.InvoiceType),
            new SqlParameter("@ExpressDeliveryNo",model.ExpressDeliveryNo),
            new SqlParameter("@DespatchDate",model.DespatchDate),
            new SqlParameter("@SignInDate",model.txtSignInDate)
        };
        return help.RunProcInt("InvoiceAdd", prams);
    }

    public DataTable GetDetail(string strID)
    {
        return help.GetDataTable("select * from M_InvoiceContract where M_InvoiceContractID='" + strID + "'");
    }

    public int InvoiceUpdate(InvoiceModel model)
    {
        string date = System.DateTime.Now.ToString("yyyy-MM-dd hh:mm");
        SqlParameter[] prams = new SqlParameter[]{
            new SqlParameter("@M_InvoiceContractID",model.M_InvoiceContractID),
            new SqlParameter("@ContractNoMain",model.ContractNoMain),
            new SqlParameter("@SONo",model.SoNo),
            new SqlParameter("@UserCode",model.UserCode),
            new SqlParameter("@date",date),
            new SqlParameter("@InvoiceContractAmount",model.InvoiceContractAmount),
            new SqlParameter("@InvoiceType",model.InvoiceType)
        };
        return help.RunProcInt("InvoiceUpdate", prams);
    }

    public DataTable InvoiceFilesAdd(InvoiceModel model)
    {
        SqlParameter[] prams = new SqlParameter[]{
            new SqlParameter("@InvoiceNo",model.InvoiceNo),
            new SqlParameter("@InvoiceFileName",model.InvoiceFileName),
            new SqlParameter("@UserCode",model.UserCode),
            new SqlParameter("@date",model.date)
        };
        return help.RunDataTableProc("InvoiceFilesAdd", prams);
    }

    public DataTable[] GetInvoiceContractFiles(string invoiceNo)
    {
        return help.RunProcGetTables("GetInvoiceContract", new SqlParameter[] { new SqlParameter("@InvoiceNo", invoiceNo) });
    }

    public DataTable InvoiceFilesDelete(string invoiceNo)
    {
        return help.RunDataTableProc("InvoiceFilesDelete", new SqlParameter[] { new SqlParameter("@InvoiceNo", invoiceNo) });
    }
}